package com.cc.servlet;

import com.alibaba.fastjson2.JSON;
import com.cc.entity.Page;
import com.cc.entity.Transactions;
import com.cc.service.TransactionsService;
import com.cc.service.impl.TransactionsServiceImpl;
import com.cc.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class transactionsServlet extends HttpServlet {
    private TransactionsService transactionsService=new TransactionsServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置访问编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("utf-8");
        //获取请求参数
        String method = req.getParameter("method")==null?"list":req.getParameter("method");
        switch (method){
            case "list":
                list(req,resp);
                break;
            case "delete":
                delete(req,resp);
                break;
            case "edit":
                edit(req,resp);
                break;
        }
    }

    /**
     * 删除
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        boolean flag = transactionsService.delete(Integer.parseInt(id));
        Map<String,String> map = new HashMap<>();
        if (flag){
            map.put("code","200");
            map.put("msg","删除成功");
        }else {
            map.put("code","500");
            map.put("msg","删除失败");
        }
        resp.getWriter().write(JSON.toJSONString(map));
    }

    /**
     * 编辑
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端参数
        String id=req.getParameter("id");
        String userId=req.getParameter("userId");
        String accountId=req.getParameter("accountId");
        String categoryId=req.getParameter("categoryId");
        String amount=req.getParameter("amount");
        String type=req.getParameter("type");

        String description=req.getParameter("description");
        String transaction_date=req.getParameter("transaction_date");
        String createdAt=req.getParameter("createdAt");
        Transactions transactions=new Transactions();
        if (id!=null||!"".equals(id)){
            transactions.setId(Integer.parseInt(id));
        }else {
            transactions.setId(0);
        }
        transactions.setUser_id(Integer.parseInt(userId));
        transactions.setAccount_id(Integer.parseInt(accountId));
        transactions.setCategory_id(Integer.parseInt(categoryId));
        transactions.setAmount(Double.parseDouble(amount));
        transactions.setType(type);
        transactions.setDescription(description);
        transactions.setTransaction_date(DateUtil.parseDate(transaction_date));
        transactions.setCreated_at(new java.sql.Timestamp(DateUtil.parseDate(createdAt).getTime()));
        boolean flag = transactionsService.edit(transactions);
        Map<String,String> map = new HashMap<>();
        if (flag){
            map.put("code","200");
            map.put("msg","修改成功");
        }else {
            map.put("code","500");
            map.put("msg","修改失败");
        }
        resp.getWriter().write(JSON.toJSONString(map));

    }

    /**
     * 分页列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //当前页码
        String pageNum=req.getParameter("pageNum");
        //搜索用户名
        String name=req.getParameter("name");
        if (pageNum==null){
            pageNum="1";
        }
        Page<Transactions> page=new Page<Transactions>();
        page.setPageNum(Integer.parseInt(pageNum));
        page=transactionsService.getTransactionsList(page, Integer.parseInt(name));
        String jsonString = JSON.toJSONString(page, "yyyy-MM-dd");
        resp.getWriter().write(jsonString);

    }
}
